<?php							
		class CItems extends database
		{
			var $table = 'cdb_items';
			
			public function CItemsSearchResult($keyWord)
			{
				$sql="select id,name,manufacture,price,id_type,picture,isNew,status,id_items from $this->table where name like '%$keyWord%' or id_items like '%$keyWord%' order by name";
				$this->setQuery($sql);
				$this->query();
				$arr = $this->loadAllRow();
				
				$numOfRow = mysql_affected_rows();
				
				if($numOfRow>0)
					return $arr;
				else
					return -1;				
			}
			
			
			public function CItemsListOfType($idType)
			{
				
				$sql="select * from {$this->table} where id_type= '{$idType}'";
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();
				
	
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
			}
			
			public function CItemListOfTypeLimited($idType, $limit=0)
			{
				if($limit == 0 ){
					$sql="select * from {$this->table} where id_type= '{$idType}' order by isNew desc";
				}
				else{
					$sql="select * from {$this->table} where id_type= '{$idType}' order by isNew desc limit 0 ,$limit";
				}
				
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();
				
	
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
			}

			public function CItemsOfType ($id_type)
			{
				$sql = "select id,name,manufacture,price,id_type,picture,isNew,status,id_items from {$this->table} where id_type = '{$id_type}'";
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();		
			
				
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
					
			}
			
			public function CItemsCount()
			{
				$sql = "select count(*) as AI from {$this->table}";
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();		
			
				
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp[0]['AI']; 
				else
					return -1;
			}
			public function CItemsLimitList($pageIndex)
			{
				$fromIdx=16*$pageIndex;
				$toIdx=$fromIdx+16;
				
				$sql = "select id,name,manufacture,price,id_type,picture,isNew,status,id_items from {$this->table} LIMIT {$fromIdx},{$toIdx}"	;
				$this->setQuery($sql);
				$this->query();
				$arrTemp = $this->loadAllRow();		
	
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
			
			}
			public function CItemsList ()
			{
				$sql = "select id,name,manufacture,price,id_type,picture,isNew,status,id_items from {$this->table} "	;
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();
				
	
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
			}
			
			public function CItemsListActive()
			{
				$sql = "select id,name,manufacture,price,id_type,picture,isNew,status,id_items from {$this->table} where status=1 order by name"	;
				$this->setQuery($sql);
				$this->query();
				$arrTemp =  $this->loadAllRow();
				
	
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arrTemp; 
				else
					return -1;
			}
			
			
			public function CItemsDetail ($id)
			{
				$sql="select * from $this->table where id='{$id}'";
				$this->setQuery($sql);
				$this->query();
				$arr=$this->loadAllRow();
				
				$numOfRow = mysql_affected_rows();
				if($numOfRow>0)
					return $arr[0];
				else
					return -1;
			}
			
			
			
			public function CItemsAdd ($id_type,$name,$price,$status_price,$status_sell,$status,$picture,$favorite,$isNew,$description,$manufacture,$thumbnail,$script_performance,$id_items)
			{
				$sql = "INSERT INTO {$this->table} (`id_type`,`name`,`price`,`status_price`,`status_sell`,`status`,`picture`,`favorite`,`isNew`,`description`,`manufacture`,`thumbnail`,`script_performance`,`id_items`)
	VALUES ('{$id_type}','{$name}','{$price}','{$status_price}','{$status_sell}','{$status}','{$picture}','{$favorite}','{$isNew}','{$description}','{$manufacture}','{$thumbnail}','{$script_performance}','{$id_items}');";
				
				

				$this->setQuery($sql);
				$this->query();	
				
				return mysql_affected_rows();
				
			}
			
			public function CItemsUpdate ($id,$id_type,$name,$price,$status_price,$status_sell,$status,$picture,$description,$manufacture,$script_performance,$id_items)
			{
				if($picture  === "" && $description !="")
				{
					$sql = "update $this->table set id_type ='{$id_type}',name ='{$name}',price ='{$price}',status_price ='{$status_price}',status_sell ='{$status_sell}',status ='{$status}',description ='{$description}',manufacture ='{$manufacture}',script_performance ='{$script_performance}' ,id_items ='{$id_items}'  where id = '{$id}' ";
				}
				if($description ==="" && $picture  != "")
				{
					$sql = "update $this->table set id_type ='{$id_type}',name ='{$name}',price ='{$price}',status_price ='{$status_price}',status_sell ='{$status_sell}',status ='{$status}',picture ='{$picture}',manufacture ='{$manufacture}',script_performance ='{$script_performance}' ,id_items ='{$id_items}'  where id = '{$id}' ";
				}
				
				if($picture ==="" && $description  === "")
				{
					$sql = "update $this->table set id_type ='{$id_type}',name ='{$name}',price ='{$price}',status_price ='{$status_price}',status_sell ='{$status_sell}',status ='{$status}',manufacture ='{$manufacture}',script_performance ='{$script_performance}' ,id_items ='{$id_items}'  where id = '{$id}' ";
				}
				
				if($description !="" && $picture  != "")
				{
					$sql = "update $this->table set id_type ='{$id_type}',name ='{$name}',price ='{$price}',status_price ='{$status_price}',status_sell ='{$status_sell}',status ='{$status}',picture ='{$picture}',description ='{$description}',manufacture ='{$manufacture}',script_performance ='{$script_performance}' ,id_items ='{$id_items}'  where id = '{$id}' ";
				}
				
				
			
				$this->setQuery($sql);
				$this->query();
			
				return mysql_affected_rows();
			}
			
			
			
			public function CItemsDelete($id)
			{
				$sql="Delete from  $this->table where id='{$id}'";
				$this->setQuery($sql);
				$this->query();
			
				return mysql_affected_rows();
			}
			
			
			
			/*
			*
			*	Man chinh sua cai performance cua moi items o day nha
			*
			*/
			public function CItemsPackOfBlock($items, $perform_sytle,$width4SubTable)
			{
				
				$block ="";
				$widthOfImg = $width4SubTable - 20;
				// hien thi theo dang perform_type
				if($perform_sytle == 1)
				{
					// hien thi theo dang table doc 
					$block.="<table width='".$width4SubTable."px' class='my_command_body' cellspacing='10' border='0' style='margin-bottom: 10px;'> 			
								<tr class='DLHeadBlock' height='20px'>
									<td>
									&nbsp;&nbsp;<b>".$items['name']."</b>
									</td>
								</tr>
								<tr>
									<td align='center'>
									<a href='products.php?jmp=idb".$items['id']."&to=".$items['name']."&with=detail'>
										<img class='DLIcon' src='./applications/resources/".$items['picture']."'  border='0'>
									</a>
									</td>
								</tr>
								<tr height='36px' valign='top'>
									<td>
										<table class='DLBodyBlock' width='100%'>
										<tr>
											<td>
											ID
											</td>
											<td>
												<b>".$items['id_items']."</b>
											</td>
											<td align='right'>
												<a href='products.php?jmp=idb".$items['id']."&to=".$items['name']."&with=detail'> <img src='images/detail.png' title='View' style='cursor:pointer'/> </a> 
											</td>
										</tr>
										<tr style='display:none'>
											<td colspan='3'>
											</td>
										</tr>
										</table>
									</td>
								</tr>
								</table>
								";
					return $block;
					
				}
				if($perform_sytle == 2)
				{
					
				}
				
				
				
				
			}
		}
	
	
	
	?>
